package cn.com.cennavi.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import cn.com.cennavi.config.MineLabODConfig;

public class ModelInPutUtil {
	/**
	 * 获取指定数据类型、指定时间范围内的数据集合
	 * 
	 * @param spatialFlag2
	 * @param province
	 * @param meshCountyMap
	 * @throws Exception
	 */
	public static HashMap<String, String> getFilePaths(MineLabODConfig mineLabODConfig, Map<Integer, Set<Integer>> meshCountyMap) throws Exception {
		// TODO Auto-generated method stub

		int nowMeshCode = -1;
		int nowCountyCode = -1;
		int nowcityCode = -1;
		int nowprovinceCode = -1;

		// 0.代表按区县代码选取数据
		// 1代表按照城市名选取数据
		// 2代表按照省份选取数据
		// 3代表二次网格
		switch (mineLabODConfig.spatialFlag) {
		case 0:
			// 根据区县代码生成城市代码和省代码
			nowCountyCode = mineLabODConfig.countyCode;
			nowcityCode = mineLabODConfig.countyCode / 100 * 100;
			nowprovinceCode = mineLabODConfig.countyCode / 10000 * 10000;

			if (nowprovinceCode == 110000 || nowprovinceCode == 310000 || nowprovinceCode == 120000 || nowprovinceCode == 500000) {
				nowcityCode = nowprovinceCode;
			}
			mineLabODConfig.cityCode = nowcityCode;
			mineLabODConfig.province = nowprovinceCode;
			// 其中轨迹数据百度是全国一天的数据放在一张表，stl是一个城市一张表
			return getfilePath(mineLabODConfig.startDate, mineLabODConfig.endDate, mineLabODConfig.dataSourse, mineLabODConfig.dataClass, nowMeshCode, nowCountyCode, nowcityCode, nowprovinceCode, mineLabODConfig.spatialFlag);
		case 1:
			// 根据城市代码生成省代码
			nowcityCode = mineLabODConfig.cityCode;
			nowprovinceCode = mineLabODConfig.cityCode / 10000 * 10000;
			if (nowprovinceCode == 110000 || nowprovinceCode == 310000 || nowprovinceCode == 120000 || nowprovinceCode == 500000) {
				nowcityCode = nowprovinceCode;
			}
			mineLabODConfig.province = nowprovinceCode;
			return getfilePath(mineLabODConfig.startDate, mineLabODConfig.endDate, mineLabODConfig.dataSourse, mineLabODConfig.dataClass, nowMeshCode, nowCountyCode, nowcityCode, nowprovinceCode, mineLabODConfig.spatialFlag);
		case 2:
			// 根据省代码去筛选数据
			nowprovinceCode = mineLabODConfig.province;
			return getfilePath(mineLabODConfig.startDate, mineLabODConfig.endDate, mineLabODConfig.dataSourse, mineLabODConfig.dataClass, nowMeshCode, nowCountyCode, nowcityCode, nowprovinceCode, mineLabODConfig.spatialFlag);
		case 3:
			HashMap<String, String> nowFiles = new HashMap<String, String>();
			// 根据二次网格生成区县代码，根据区县代码生成城市代码和省代码,二次网格对应多个区县代码
			Set<Integer> countySet = meshCountyMap.get(mineLabODConfig.meshCode);
			String filePaths = "";
			if (countySet != null) {
				for (Integer county : countySet) {
					nowMeshCode = mineLabODConfig.meshCode;
					nowCountyCode = county;
					nowcityCode = county / 100 * 100;
					nowprovinceCode = county / 10000 * 10000;
					if (nowprovinceCode == 110000 || nowprovinceCode == 310000 || nowprovinceCode == 120000 || nowprovinceCode == 500000) {
						nowcityCode = nowprovinceCode;
					}
					mineLabODConfig.countyCode = nowCountyCode;
					mineLabODConfig.cityCode = nowcityCode;
					mineLabODConfig.province = nowprovinceCode;
					HashMap<String, String> nowFile = getfilePath(mineLabODConfig.startDate, mineLabODConfig.endDate, mineLabODConfig.dataSourse, mineLabODConfig.dataClass, nowMeshCode, nowCountyCode, nowcityCode, nowprovinceCode, mineLabODConfig.spatialFlag);
					nowFiles.putAll(nowFile);
				}
			}
			return nowFiles;
		}
		return null;

	}

	public static boolean testExists(String hdfsPath) throws Exception {

		Configuration conf = new Configuration();

		FileSystem hdfs = FileSystem.get(conf);
		Path dst = new Path(hdfsPath);

		boolean ok = hdfs.exists(dst);
//		System.out.println(ok ? "文件存在" : "文件不存在");
		return ok;
	}

	private static HashMap<String, String> getfilePath(String startDate, String endDate, String dateSourse1, String dataClass1, int meshCode, int countyCode, int cityCode, int province, int spatialFlag) throws Exception {
		// TODO Auto-generated method stub
		// StringBuffer sb = new StringBuffer();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date sdate = sf.parse(startDate);
		Date edate = sf.parse(endDate);
		if (sdate.after(edate))
			return null;
		// 按照城市名选取数据
		Calendar start = Calendar.getInstance();
		start.setTime(sdate);
		Long startTIme = start.getTimeInMillis();

		Calendar end = Calendar.getInstance();
		end.setTime(edate);
		Long endTime = end.getTimeInMillis();

		Long oneDay = 1000 * 60 * 60 * 24l;
		String[] dateSourseArr = dateSourse1.split("\\|");
		String[] dataClassArr = dataClass1.split("\\|");

		// key:路径，value：数据源数据类型
		HashMap<String, String> returnStr = new HashMap<String, String>();
		for (int j = 0; j < dateSourseArr.length; j++) {
			String dateSourse = dateSourseArr[j];
			String dataClass = dataClassArr[j];

			Long time = startTIme;
			while (time <= endTime) {
				Date d = new Date(time);
				// 采用的是stl轨迹数据
				if ("STL".equals(dataClass) && "trajectory".equals(dateSourse)) {
					String cityCode1 = cityCode + "";
					if (spatialFlag == 2) {
						cityCode1 = province / 10000 + "*";
					}
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(d);
					String files = "hdfs://nujhadoop/BasicTrajectory/" + dataClass + "/" + calendar.get(calendar.YEAR) + "/c=" + cityCode1 + "/m=" + String.format("%02d", calendar.get(calendar.MONTH) + 1) + "/d=" + String.format("%02d", calendar.get(calendar.DATE)) + "/w=" + calendar.get(calendar.DAY_OF_WEEK) + "/BASIS_TRAJECTORY*";
					String filesPath = "hdfs://nujhadoop/BasicTrajectory/" + dataClass + "/" + calendar.get(calendar.YEAR) + "/c=" + cityCode1 + "/m=" + String.format("%02d", calendar.get(calendar.MONTH) + 1) + "/d=" + String.format("%02d", calendar.get(calendar.DATE)) + "/w=" + calendar.get(calendar.DAY_OF_WEEK);
//					System.out.println(files);
					if (spatialFlag != 2) {
						if (testExists(filesPath)) {
							returnStr.put(filesPath, dateSourse + "_" + dataClass);

						}
					} else {
						boolean isGood = false;
						for (int i = 0; i < 30; i++) {
							int nowCity = province + i * 100;
							filesPath = "hdfs://nujhadoop/BasicTrajectory/" + dataClass + "/" + calendar.get(calendar.YEAR) + "/c=" + nowCity + "/m=" + String.format("%02d", calendar.get(calendar.MONTH) + 1) + "/d=" + String.format("%02d", calendar.get(calendar.DATE)) + "/w=" + calendar.get(calendar.DAY_OF_WEEK);
							if (testExists(filesPath)) {
								isGood = true;
							}
						}
						if (isGood) {
							returnStr.put(filesPath, dateSourse + "_" + dataClass);
						}
					}
				} else {
					// 扩展
					// 采用的是百度轨迹数据
					if ("BAIDU".equals(dataClass) && "trajectory".equals(dateSourse)) {
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(d);
						String files = "hdfs://nujhadoop/BasicTrajectory/" + dataClass + "/" + calendar.get(calendar.YEAR) + "/m=" + String.format("%02d", calendar.get(calendar.MONTH) + 1) + "/d=" + String.format("%02d", calendar.get(calendar.DATE)) + "/BASIS_TRAJECTORY*";
						String filePath = "hdfs://nujhadoop/BasicTrajectory/" + dataClass + "/" + calendar.get(calendar.YEAR) + "/m=" + String.format("%02d", calendar.get(calendar.MONTH) + 1) + "/d=" + String.format("%02d", calendar.get(calendar.DATE));
//						System.out.println(files);
						if (testExists(filePath)) {
							returnStr.put(filePath, dateSourse + "_" + dataClass);
						}
					}

				}
				time += oneDay;
			}
		}

		if (returnStr == null || returnStr.size() == 0)
			return null;
		return returnStr;
	}

	public static void main(String[] args) {

	}
}
